﻿using System;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;
namespace CommonLib.Extension
{
	public static class JsonExtension
	{
		static public T ReadJsonFile<T>(string path)
		{
			try
			{
				if (File.Exists(path))
				{
					string content = File.ReadAllText(path);
					//T re = JsonConvert.DeserializeObject<T>(content);
					return ReadJson<T>(content);
				}
				else
				{
					//TODO
					Debug.LogError($"未找到文件，文件路径{path}");
				}
			}
			catch (Exception e)
			{
				//TODO
				Debug.LogError($"转换Json字符串失败，文件路径 ：{path}");
			}
			return default;
		}

		static public T ReadJson<T>(string jsonString)
		{
			try
			{
				T re = JsonConvert.DeserializeObject<T>(jsonString);
				return re;
			}
			catch (Exception e)
			{
				//TODO
				Debug.LogError($"反序列化字符串失败, 异常信息 ：{e.StackTrace} \n 字符串内容 :{jsonString}");
			}
			return default;
		}
	}
}